package com.ukao.cashregister.dialog;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.eventbus.UserInfoEvent;
import com.ukao.cashregister.listener.OnDialogSureClickListener;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.widget.StateImageView;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.bingoogolapple.qrcode.core.DisplayUtils;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

/**
 * 修改名字
 * Created by ukaodev on 2017/6/2.
 */

public class QuiltSweepDialogFragment extends BaseDialogFragment {


    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.close_btn)
    StateImageView closeBtn;
    @BindView(R.id.txt_shouldpice)
    TextView txtShouldpice;
    @BindView(R.id.cdCode)
    ImageView cdCode;
    @BindView(R.id.txt_hite)
    TextView txtHite;
    private String balance="0";
    private String codeUrl;
    private String shouldPice="0";
    private int entertype;
    private Unbinder unbinder;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        codeUrl = getArguments().getString(ARG_PARAM1);
        shouldPice = getArguments().getString(ARG_PARAM2,"0");
        entertype = getArguments().getInt(ARG_PARAM4);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_quilt_sweep_fragment, container, false);
        unbinder= ButterKnife.bind(this, view);
        getDialog().setCanceledOnTouchOutside(false);
        initView();


        return view;
    }

    private void initView() {
        title.setText(entertype == 0 ? "支付宝支付" : "微信支付");
        txtShouldpice.setText("¥ " + shouldPice);
        createQRCode(codeUrl);
    }

    private OnDialogSureClickListener mOnDialogSureClickListener;

    public void setOnSureClickListener(OnDialogSureClickListener mOnDialogSureClickListener) {
        this.mOnDialogSureClickListener = mOnDialogSureClickListener;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void createQRCode(String code) {

        QRCodeEncoder.encodeQRCode(code, DisplayUtils.dp2px(activity, 330), new QRCodeEncoder.Delegate() {

            @Override
            public void onEncodeQRCodeSuccess(Bitmap bitmap) {
                cdCode.setImageBitmap(bitmap);
            }


            @Override
            public void onEncodeQRCodeFailure() {
                T.show("生成二维码失败");
            }
        });
    }


    @Override
    protected boolean useEventBus() {
        return false;
    }

    @Subscribe
    public void onEvent(UserInfoEvent event) {

    }

    @OnClick({R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close_btn:
                dismiss();
                break;

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
            /*解锁*/
         if (mOnDialogSureClickListener!=null){
             mOnDialogSureClickListener.onSureItemClick("");
         }


    }
}
